<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Project extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'project';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'open_time_text',
        'review_time_text'
    ];
    
    //更新项目状态
    public function updateProjectStatus($pid){
        $row = $this->where('id', $pid)->find();
        $data_p = [];
        // if ($row['status'] != 3) {
           
        // }
        $yg_num = $row['num'];//需求专家数量
        $real_num = Db::name('projectexpert')->where('project_id', $pid)->where('status',2)->count();//已确认回复同意的数量  
        $data_p['agree_num'] = $real_num;
        if ($real_num >= $yg_num) {
            $data_p['status'] = 3;
        } else {
            $data_p['status'] = 2;
        }
        $this->where('id', $pid)->update($data_p);//更新已同意数量
    }



    public function getOpenTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['open_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReviewTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['review_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setOpenTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReviewTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
